const fs = require("fs");
const UserService = require("../service/user.service");
const fileService = require("../service/file.service");
const { UPLOAD_PATH } = require("../config/path");

class UserController {
  async create(ctx, next) {
    // 1、获取用户信息
    const user = ctx.request.body;
    // 2、将user信息存储到数据库中
    const result = await UserService.create(user);
    // 3、查看存储结果，告知前端创建成功
    ctx.body = {
      message: "创建用户成功~",
      data: result,
    };
  }

  async showAvatarImage(ctx, next) {
    const { userId } = ctx.params;
    const result = await fileService.queryAvatarById(userId);
    const { filename, mimetype } = result;
    ctx.type = mimetype;
    ctx.body = fs.createReadStream(`${UPLOAD_PATH}/${filename}`);
  }
}

module.exports = new UserController();
